package du.zhaofeng2019;

import android.content.Context;
import android.content.Intent;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

public class TreeDataProvider implements DataProvider {


    String TAG = getClass().getSimpleName();

    private Context context;
    private ArrayList<TreeElement> nodes = new ArrayList<>();


    public TreeDataProvider(Context context) {
        super();
        this.context = context;
    }


    private Intent getIntent(Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        return intent;
    }


    @Override
    public ArrayList<TreeElement> getDataSource() {
        //   if (null == nodes || nodes.size() == 0) //修改数据，需要刷新
        nodes = new ArrayList<>();
        initDataTree();
        return nodes;
    }

    private void initDataTree() {

        TreeElement elementUnit;
        TreeElement elementDept;
        TreeElement elementName;


        //第一层：查询 所有的学校
        SqlMethod sqlMethod = new SqlMethod(context);
        LinkedHashMap<String, String> idUnitMap = sqlMethod.findIdUnitMap();

        // 如：没有学校
        if (idUnitMap == null) {
            return;
        }

        //添加所有的学校
        for (Map.Entry<String, String> idUnitEntry : idUnitMap.entrySet()) {
            elementUnit = new TreeElement(idUnitEntry.getKey(), idUnitEntry.getValue());
            nodes.add(elementUnit);

            //循环各个学校，查询各个学校 的 部门
            LinkedHashMap<String, String> idDeptMap = sqlMethod.findIdDeptMap(elementUnit.getTitle());
            for (Map.Entry<String, String> idDeptEntry : idDeptMap.entrySet()) {
                elementDept = new TreeElement(idDeptEntry.getKey(), idDeptEntry.getValue());
                elementUnit.addChild(elementDept);

                // 循环各个部门，查询各个部门 中 教师的姓名
                LinkedHashMap<String, String> idNameMap = sqlMethod.findIdNameMap(elementUnit.getTitle(), elementDept.getTitle());
                for (Map.Entry<String, String> idNameEntry : idNameMap.entrySet()) {
                    elementName = new TreeElement(idNameEntry.getKey(), idNameEntry.getValue());
                    elementDept.addChild(elementName);
                }

            }
        }


    }


    @Override
    public void foward(String caption) throws Exception {

    }


}
